import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
// 演示在 flutter 中打开项目内 assets 下 h5 页面
class FlutterH5JumpAsset extends StatefulWidget {
  const FlutterH5JumpAsset({super.key});

  @override
  State<FlutterH5JumpAsset> createState() => _FlutterH5JumpAssetState();
}

class _FlutterH5JumpAssetState extends State<FlutterH5JumpAsset> {
  late WebViewController controller;

  get _loadBtn => FilledButton(onPressed: (){
    _onLoadFlutterAssets(context);
  }, child: const Text('加载项目内H5页面',style: const TextStyle(color: Colors.white),));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('打开项目内H5页面'), actions: [_loadBtn],),
      body: WebViewWidget(controller: controller),
    );
  }

  @override
  void initState() {
    super.initState();
    controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted);
  }

  void _onLoadFlutterAssets(BuildContext context) async {
    // 注意 assets 目录或者其下的文件有变动都需要重新运行项目
    await controller.loadFlutterAsset('assets/hiH5/index.html');
  }
}
